import { Application } from 'egg';
import {
  indexRule,
  createRule,
  updateRule,
  destroyRule,
  attrValRule
 } from '../validate/goods/attr';

/**
 * 商品属性片管理
 */
export default (app: Application) => {
  const { auth } = app.middleware;
  const { jwt } = app;

  return {
    'goods.attr.all': {
      method: 'GET',
      path: '/api/goods/attr/all',
      options: {
        tags: ['attr/list/all'],
        description: '商品属性列表',
        handler: app.controller.goods.attr.all,
        middlewares: [jwt, auth('goods.attr.all')],
        validate: indexRule,
      },
    },
    'goods.attr.list': {
      method: 'GET',
      path: '/api/goods/attr',
      options: {
        tags: ['attr/list'],
        description: '商品属性列表',
        handler: app.controller.goods.attr.list,
        middlewares: [jwt, auth('goods.attr.list')],
        validate: indexRule,
      },
    },
    'goods.attr.create': {
      method: 'POST',
      path: '/api/goods/attr',
      options: {
        tags: ['attr/create'],
        description: '创建商品属性',
        handler: app.controller.goods.attr.create,
        middlewares: [jwt, auth('goods.attr.create')],
        validate: createRule,
      },
    },
    'goods.attr.update': {
      method: 'PUT',
      path: '/api/goods/attr/:id',
      options: {
        tags: ['attr/update'],
        description: '更新商品属性',
        handler: app.controller.goods.attr.update,
        middlewares: [jwt, auth('goods.attr.update')],
        validate: updateRule,
      },
    },
    'goods.attr.create.val': {
      method: 'POST',
      path: '/api/goods/attr/val/:id',
      options: {
        tags: ['attr/val/create'],
        description: '新增商品属性值',
        handler: app.controller.goods.attr.val,
        middlewares: [jwt, auth('goods.attr.val')],
        validate: attrValRule,
      },
    },
    'goods.attr.destroy': {
      method: 'DELETE',
      path: '/api/goods/attr/:id',
      options: {
        tags: ['attr/delete'],
        description: '删除商品属性',
        handler: app.controller.goods.attr.destroy,
        middlewares: [jwt, auth('goods.attr.destroy')],
        validate: destroyRule,
      },
    }
  };
};
